// var bShow = false;
// var bExpand = true;
// var hOrig;
var isMove = 1;
// var pagePos = 0;
var startTime;
var timer;
var isScrolling = false;
var isScrollingOnTouchstart = false;

function onScroll() {
    isScrolling = true;
    
    clearTimeout(timer);
    
    timer = setTimeout(function () {
        isScrolling = false;
    }, 50);
}

document.addEventListener('touchmove', onScroll, false);
document.addEventListener('scroll', onScroll, false);

/** 判断当前点击的div是否在财报的table里面 */
function isInTable (obj){
    if(!document.getElementsByClassName('table-wrap').length>0)return;
    while (obj != undefined && obj != null && obj.tagName.toUpperCase() != 'BODY'){
        if (obj.className=='table-wrap'){
            return true;
        }
        obj = obj.parentNode;
    }
    return false;
}

/** 判断新闻正文页是否支持左滑和右滑 ontouchstart（不支持左右手势滑动） ontouchend（支持左右手势滑动） ontouchmove（不支持左右手势滑动）*/
function newsCanSupportPanRecognizer(e,isSupport){
    var _target = e.target;
    if(isInTable(_target)){
        NewsDetail.newsCanSupportPanRecognizer(isSupport);
    }
}

document.ontouchstart = function (e) {
    newsCanSupportPanRecognizer(e,"0");
    isMove = 1;
    isScrollingOnTouchstart = isScrolling;
    startTime = (new Date()).getTime();
    document.documentElement.style.webkitTapHighlightColor = "rgba(1,1,1,1)";
    /*webkit api*/
};

document.ontouchend = function (e) {
    newsCanSupportPanRecognizer(e,"1");
    var last = ( new Date() ).getTime() - startTime;
    var target = e.target;
    
    // 长按忽略
    if (last > 500) {
        return;
    }
    
    // e.target returns the text node in iOS 4.3 when you click text and then e.target.tagName returns undefined.
    while (target.nodeType == 3) {
        target = target.parentNode;
    }
    
    if (isMove == 1 && isScrollingOnTouchstart == false) {
        // 判断是否点击热门评论区域
        var hotComment = document.getElementById('hot_comment');
        var containOrEqual = false;
        var tempEl = target;
        
        while (tempEl) {
            if (tempEl === hotComment) {
                containOrEqual = true;
                break;
            }
            tempEl = tempEl.parentNode;
        }
        
        if (containOrEqual) {
            document.location = tempEl.id;
        }
        else if (target.tagName.toLowerCase() == "img") {
            document.location = target.id;
        }
        //                                // 直播链接
        //                                else if( target.tagName.toLowerCase() == 'div' &&
        //                                        target.id == 'live'){
        //                                    var id = 'live';
        //                                    var liveId = target.getAttribute( 'data-live-id' );
        //                                    var liveType = target.getAttribute( 'data-type' );
        //                                    document.location = '?id=' + id + '&liveid=' +  liveId + '&livetype=' + liveType;
        //                                }
        else if (target.tagName.toLowerCase() == "li") {
            document.location = target.id;
            var i = 0;
            while (i < target.childNodes.length) {
                var node = target.childNodes[i];
                if (node.tagName.toLowerCase() == "a") {
                    node.className = 'visited';
                }
                i++;
            }
            
        }
        else if (target.tagName.toLowerCase() == "a") {
            // 相关新闻中的A标签
            if (target.id) {
                document.location = target.id;
            }
            //                                     // 正文中的A标签
            //                                    else
            //                                    {
            //                                         document.location = "openbrowser:" + target.getAttribute( 'href' );
            //                                    }
            target.className = 'visited';
            //                                    e.preventDefault();
        }
        else {
            document.location = "touched";
        }
    }
};
document.ontouchmove = function (e) {
    newsCanSupportPanRecognizer(e,"0");
    isMove = 2;
};
/* window.onscroll = function()
 {
 document.location = ( document.body.scrollTop - pagePos ) < 0 ? "scrollUp" : "scrollDown";
 pagePos = document.body.scrollTop;
 }
 */
//                        function changeLoadedImage(id, loadedUrl) {
//                            var img = document.getElementById(id);
//
//                            if (img) {
//                                img.src = loadedUrl;
//
//                                var target = document.getElementById("groupImageSig");
//                                if (target) {
//                                    target.style.display = "";
//                                }
//                            }
//                        }
function toggle() {
    // var node = document.getElementById('stockref');
    // var node2 = document.getElementById('stockbtn');
    // node.style.overflow = 'hidden';
    // var h = node.scrollHeight;
    // if (h > 90 && !bShow) {
    //     bShow = true;
    //     hOrig = h;
    // }
    // else {
    //     node.style.webkitTransition = '0.2s ease-in';
    // }
    // if (bShow) {
    //     if (bExpand) {
    //         bExpand = false;
    //         node.style.height = '90px';
    //         while (node2.hasChildNodes()) {
    //             node2.removeChild(node2.childNodes[0]);
    //         }
    //         var textNode = document.createTextNode('展开 ');
    //         node2.appendChild(textNode);
    //         var imgNode = document.createElement('img');
    //         imgNode.src = "stock_expand@2x.png";
    //         imgNode.style.width = '11px';
    //         imgNode.style.height = '7px';
    //         node2.appendChild(imgNode);
    //     }
    //     else {
    //         bExpand = true;
    //         node.style.height = hOrig + 'px';
    //         while (node2.hasChildNodes()) {
    //             node2.removeChild(node2.childNodes[0]);
    //         }
    //         var textNode = document.createTextNode('收起 ');
    //         node2.appendChild(textNode);
    //         var imgNode = document.createElement('img');
    //         imgNode.src = "stock_close@2x.png";
    //         imgNode.style.width = '11px';
    //         imgNode.style.height = '7px';
    //         node2.appendChild(imgNode);
    //     }
    // }
};

var callNewsRelatedDetail = function (newsUrl) {
    //var detailInfo = JSON.stringify({"url": newsUrl});
    NewsDetail.newsRelatedDetail(newsUrl);
}

var callMediaArticles = function () {
    NewsDetail.mediaArticles();
}

var callSubscibe = function (authorID) {
    NewsDetail.subscribeArticles(authorID);
}

var callTopFinanceSearch = function (searchKey) {
    NewsDetail.topFinanceSearch(searchKey);
}

function loadSwiper() {
    var mySwiper = new Swiper('.swiper-container', {
    autoplay: 3000,
    autoplayDisableOnInteraction: false,
    loop: true
    });
}

function relareConceptChildClick(stockDetail) {
    NewsDetail.newsStockDetail(stockDetail);
}

function infoLiveStockTagClick(stockDetail) {
    NewsDetail.news7x24StockClick(stockDetail);
}

function status() {
    return "OK";
}

function readOriginal(){
    NewsDetail.newsReadOriginal();
}

